package cn.shyouchi.wuguanjia.android.ui.event.meet;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventMeetActivity extends BaseActivity implements EventMeetContract.View {


    public final static int REQUEST_TYPE_ADD = 10001;
    public final static int REQUEST_TYPE_ADD_SUCCESS = 10002;

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_meet)
    RecyclerView mRvMeet;
    @BindView(R.id.btn_add_meet)
    Button mBtnAddMeet;
    EventMeetPresenter mPresenter;
    String projectId;

    List<EventMeetEntity.ResultMessageBean> mMeetEntities = new ArrayList<>();
    MeetAdapter mMeetAdapter;


    boolean needRefresh = true;

    @Override
    public int initContentView() {
        return R.layout.activity_event_meet;
    }

    @Override
    public void initInjector() {
        mPresenter = new EventMeetPresenter(mContext, this);
        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);
        mMeetAdapter = new MeetAdapter(mMeetEntities, this);
        mRvMeet.setLayoutManager(new LinearLayoutManager(mContext));
        mRvMeet.setAdapter(mMeetAdapter);
        mPresenter.getMeetList(projectId);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }



    @OnClick(R.id.btn_add_meet)
    public void onClick() {
        Intent intent = new Intent(mContext, AddMeetActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD);
    }

    @Override
    public void onDataRefresh(List<EventMeetEntity.ResultMessageBean> resultMessageBeanList) {
        mMeetEntities.clear();
        mMeetEntities.addAll(resultMessageBeanList);
        mMeetAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(EventMeetEntity.ResultMessageBean entity) {
        LogUtil.json(entity);
        Intent intent = new Intent(mContext, PreviewMeetActivity.class);
        intent.putExtra("meet", entity);
        intent.putExtra("projectId", projectId);
        startActivity(intent);
    }

    @Override
    public void onDelClick(EventMeetEntity.ResultMessageBean resultMessageBean) {
        mPresenter.delMeet(projectId, String.valueOf(resultMessageBean.getId()));
    }

    @Override
    public void onEditClick(EventMeetEntity.ResultMessageBean resultMessageBean) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD && resultCode == REQUEST_TYPE_ADD_SUCCESS) {
            mPresenter.getMeetList(projectId);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mPresenter.getMeetList(projectId);
            needRefresh = false;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }
}
